// addressManagerPackage/pages/addressList/addressList.js
const app = getApp();

const common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        addressList: [],
        searchFocus: true,
        searchFlag: false,
        //搜索
        click: 0
    },
    searchDetail(click) {
        console.log(22);
        this.setData({
            page: 1
        });
    },
    //记录搜索名称
    saveWord: function(e) {
        var that = this;
        var search_con = e.detail.value;
        that.setData({
            search_con: search_con,
            page: 1,
            goods: [],
            searchFlag: true,
            click: that.data.click + 1
        });
        console.log(that.data.click);
        that.searchDetail(that.data.click);
    },
    /**
   * 选择默认地址
   */
    chooseDefaultAddress(e) {
        let that = this;
        let idx = e.currentTarget.dataset.index;
        var addressList = that.data.addressList;
        var chooseItem;
        addressList.forEach((item, index) => {
            if (index == idx) {
                item.def = 1;
                chooseItem = item;
            } else {
                item.def = 0;
            }
        });
        that.setData({
            addressList: addressList
        });
        //保存
                that.saveAddress(chooseItem);
    },
    /**
   * 获取微信通讯地址
   */
    getWxAddress() {
        wx.chooseAddress({
            success(res) {
                console.log(res);
                var city = [ res.provinceName, res.cityName, res.countyName ];
                var area = res.detailInfo;
                var name = res.userName;
                var phone = res.telNumber;
                var item = {
                    city: city,
                    area: area,
                    name: name,
                    phone: phone,
                    def: true
                };
                var str = JSON.stringify(item);
                wx.navigateTo({
                    url: "/addressManagerPackage/pages/addressAdd/addressAdd?address=" + str
                });
            }
        });
    },
    addAddress() {
        wx.navigateTo({
            url: "/addressManagerPackage/pages/addressAdd/addressAdd"
        });
    },
    /**
   * 获取用户列表
   */
    getAddressList() {
        let that = this;
        common.request({
            endpoint: "My/myinfo/address_list",
            data: {},
            method: "post",
            success(res) {
                that.setData({
                    addressList: res
                });
            }
        });
    },
    /**
   * 删除地址
   */
    deleteAddress(e) {
        let that = this;
        var id = e.currentTarget.dataset.id;
        var idx = e.currentTarget.dataset.index;
        common.request({
            endpoint: "My/myinfo/address_del",
            data: {
                id: id
            },
            method: "post",
            success(res) {
                console.log(res);
                //前端删除数据
                                var addressList = that.data.addressList;
                addressList.splice(idx, 1);
                that.setData({
                    addressList: addressList
                });
            }
        });
    },
    /**
   * 编辑地址
   */
    editAddress(e) {
        let that = this;
        var idx = e.currentTarget.dataset.index;
        var addressList = that.data.addressList;
        addressList.forEach((item, index) => {
            console.log(1);
            if (index == idx) {
                item.def = item.def == 0 ? false : true;
                if (!item.city) {
                    item.city = "";
                }
                item.city = item.city.split("-");
                var str = JSON.stringify(item);
                wx.navigateTo({
                    url: "/addressManagerPackage/pages/addressAdd/addressAdd?address=" + str + "&&old=old"
                });
            }
        });
    },
    /**
   * 选择地址
   */
    chooseAddress(e) {
        let that = this;
        //获取前一个页面chooseAddress
                var pages = getCurrentPages();
        if (pages.length > 1) {
            console.log(pages[pages.length - 2].route == "/pages/place_order/place_order");
            var addressList = that.data.addressList;
            var index = e.currentTarget.dataset.index;
            app.globalData.reciever = {
                receiverName: addressList[index].name,
                //收货人名称
                phone: addressList[index].phone,
                //电话
                address: addressList[index].city + " " + addressList[index].area,
                //收货地址
                url: "/addressManagerPackage/pages/addressList/addressList",
                //跳转地址管理页面路径
                area: addressList[index].area,
                city: addressList[index].city,
                position: addressList[index].position,
                lng: addressList[index].position ? addressList[index].position.split(",")[0] : "",
                lat: addressList[index].position ? addressList[index].position.split(",")[1] : "",
                wwb_street_id: addressList[index].wwb_street_id
            };
            if (!app.globalData.reciever.lng || !app.globalData.reciever.lat) {
                wx.showModal({
                    title: "提示",
                    content: "该地址定位异常，请重新编辑",
                    success: function(res) {
                        that.editAddress(e);
                    }
                });
                return;
            }
            wx.navigateBack({});
        }
    },
    /**
   * 保存地址
   */
    saveAddress(data) {
        let that = this;
        common.request({
            endpoint: "My/myinfo/address_save",
            data: {
                ...data,
                do: "edit"
            },
            method: "post",
            success(res) {
                console.log(res);
                wx.showToast({
                    title: "设置默认地址成功",
                    icon: "none"
                });
            }
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        wx.authorize({
          scope: 'scope.userLocation',
          success(res){
            console.log(res,"授权成功");
          },
          fail(err){
            console.log(err,"授权失败");
          }
        })
        wx.hideShareMenu();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        this.getAddressList();
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {}
});